package com.sskj.xyft;

import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.WindowManager;

import com.gyf.barlibrary.ImmersionBar;
import com.sskj.base.api.OtherLoginEvent;
import com.sskj.base.data.User;
import com.sskj.base.model.UserViewModel;
import com.sskj.xyft.login.LoginActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;

public abstract class MVPActivity<T extends BasePresenter> extends BaseActivity {

    protected T mPresenter;

    protected String TAG;

    public static UserViewModel userViewModel;

    public static boolean isLogin;

    public static String account;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setContentView(getLayoutId());
        ImmersionBar.with(this).init();
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        mPresenter = getPresenter();
        mPresenter.attachView(this);
        TAG = this.getClass().getName();
        getLifecycle().addObserver(mPresenter);
        LiveDataBus.get().with(1, User.class)
                .observe(MyApplication.instance,user -> {
                    if (user != null) {
                        account = user.getAccount();
                    } else {
                        account = "";
                    }
                });
//        userViewModel = ViewModelProviders.of(this).get(UserViewModel.class);
//        userViewModel.getUser(this).observe(this, user -> {
//            if (user != null) {
//                account = user.getAccount();
//            } else {
//                account = "";
//            }
//        });
        if (!TextUtils.isEmpty(MyApplication.getAccount()) && !TextUtils.isEmpty(MyApplication.getToken())) {
            isLogin = true;
        } else {
            isLogin = false;
        }
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        ImmersionBar.with(this).destroy();
    }

    public void initEvent() {

    }

    public abstract int getLayoutId();

    public abstract T getPresenter();

    public abstract void initView();

    public abstract void initData();


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void otherLogin(OtherLoginEvent event) {
        new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("请先登录")
                .setCancelable(false)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        LoginActivity.start(MVPActivity.this);
                        AppManager.getInstance().finish2Login();
                    }
                }).show();

    }

}
